package top.zuli.package01.class14;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

public class ThreadNew {
    public static void main(String[] args) {
        //1.创建callable 接口
        Callable callable = new NumberThread();
        //2.创建操作任务类
        FutureTask task = new FutureTask(callable);
        //3.开启线程
        new Thread(task).start();
        //4.获取执行结果
        try {
            Object result = task.get();
            System.out.println("结果为:"+result);
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }
    }
}
class NumberThread implements Callable {
    @Override
    public Object call() throws Exception {
        int sum = 0;
        for (int i = 1; i <=100; i++) {
            sum+=i;
        }
        return sum;
    }
}

/*
public class ThreadNew {
    public static void main(String[] args) {
        Callable callable = new NumberThread();
        FutureTask task = new FutureTask(callable);
        new Thread(task).start();
        try {
            Object result = task.get();
            System.out.println("结果为:"+result);
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }
    }
}
class NumberThread implements Callable {
    @Override
    public Object call() throws Exception {
        int sum = 0;
        for (int i = 1; i <=100; i++) {
            sum+=i;
        }
        return sum;
    }
}*/

/*
public class ThreadNew {
    public static void main(String[] args) {
        Callable callable = new NumberThread();
        FutureTask task = new FutureTask(callable);
        new Thread(task).start();
        try {
            Object result = task.get();
            System.out.println("结果为:"+result);
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }
    }
}
class NumberThread implements Callable {
    @Override
    public Object call() throws Exception {
        int sum = 0;
        for (int i = 1; i <=100; i++) {
            sum+=i;
        }
        return sum;
    }
}*/
